{% extends 'site_base.html' %}

{% block head_title %}Search{% endblock %}

{% block body_id %}search_body{% endblock %}

{# wipe topics sidebar #}
{% block sidebar_topics %}{% endblock %}

{# wipe sidebar altogether #}
{% block sidebar %}{% endblock %}

{% block content_grid %}grid_12{% endblock %}

{% block body %}

<div id="searchControl" style="width:100%;">Loading...</div>
<div id="searchForm" style="width:100%;">Loading...</div>

<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">//<![CDATA[

    function getElementsByClassName(c, t, p){
    if(!p){p=document}
        var els = p.getElementsByTagName(t?t:'*');
        var re = new RegExp('\\b'+c+'\\b');
        var rels = [];
    for(var i=0;i<els.length;i++){
        var el=els[i];
        if(el.className.match(re)){
            rels.push(el)
        }
    }
    return rels;
}

    google.load('search', '1');
    function cse() {
        var sFormDiv = document.getElementById("searchForm");
        var sControlDiv = document.getElementById("searchControl");

        // create a search control
        this.sControl = new google.search.CustomSearchControl('005517685523726248670:pujuo2igaec');
        this.searchForm = new google.search.SearchForm(true, sFormDiv);

        // bind clear and submit functions
        this.searchForm.setOnSubmitCallback(this, cse.prototype.onSubmit);
        this.searchForm.setOnClearCallback(this, cse.prototype.onClear);

        // set up for large result sets
        this.sControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
        
        // set links to open in the same window

        this.sControl.setLinkTarget(GSearch.LINK_TARGET_SELF);

        var searcher;
        var options;


        // configure search control for
        searcher = new google.search.WebSearch();
        options = new google.search.SearcherOptions();
        searcher.setSiteRestriction("http://news21.com/");
        searcher.setUserDefinedLabel("News21.com");
        searcher.setUserDefinedClassSuffix("siteSearch");
        this.sControl.addSearcher(searcher, options);

        searcher = new google.search.WebSearch();
        options = new google.search.SearcherOptions();
        searcher.setSiteRestriction("http://asu.news21.com/");
        searcher.setUserDefinedLabel("Arizona State");
        searcher.setUserDefinedClassSuffix("siteSearch");
        this.sControl.addSearcher(searcher, options);

        searcher = new google.search.WebSearch();
        options = new google.search.SearcherOptions();
        searcher.setSiteRestriction("http://berkeley.news21.com/");
        searcher.setUserDefinedLabel("Berkeley");
        searcher.setUserDefinedClassSuffix("siteSearch");
        this.sControl.addSearcher(searcher, options);

        searcher = new google.search.WebSearch();
        options = new google.search.SearcherOptions();
        searcher.setSiteRestriction("http://columbia.news21.com/");
        searcher.setUserDefinedLabel("Columbia");
        searcher.setUserDefinedClassSuffix("siteSearch");
        this.sControl.addSearcher(searcher, options);

        searcher = new google.search.WebSearch();
        options = new google.search.SearcherOptions();
        searcher.setSiteRestriction("http://umd.news21.com/");
        searcher.setUserDefinedLabel("Maryland");
        searcher.setUserDefinedClassSuffix("siteSearch");
        this.sControl.addSearcher(searcher, options);

        searcher = new google.search.WebSearch();
        options = new google.search.SearcherOptions();
        searcher.setSiteRestriction("http://unc.news21.com/");
        searcher.setUserDefinedLabel("North Carolina");
        searcher.setUserDefinedClassSuffix("siteSearch");
        this.sControl.addSearcher(searcher, options);

        searcher = new google.search.WebSearch();
        options = new google.search.SearcherOptions();
        searcher.setSiteRestriction("http://news21.uscannenberg.org/");
        searcher.setUserDefinedLabel("Southern&nbsp;California");
        searcher.setUserDefinedClassSuffix("siteSearch");
        this.sControl.addSearcher(searcher, options);

        searcher = new google.search.WebSearch();
        options = new google.search.SearcherOptions();
        searcher.setSiteRestriction("http://youngandthewireless.com/");
        searcher.setUserDefinedLabel("Syracuse");
        searcher.setUserDefinedClassSuffix("siteSearch");
        this.sControl.addSearcher(searcher, options);

        searcher = new google.search.WebSearch();
        options = new google.search.SearcherOptions();
        searcher.setSiteRestriction("http://innovate.news21.com/");
        searcher.setUserDefinedLabel("Innovation");
        searcher.setUserDefinedClassSuffix("siteSearch");
        this.sControl.addSearcher(searcher, options);

        // draw the search control in tabbed mode
        var drawOptions = new google.search.DrawOptions();
        drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
        this.sControl.draw(sControlDiv, drawOptions);

        // execute a starter search
        this.searchForm.execute('{{ request.GET.q }}');
        
        // now we find and select the correct tab
        var tabs=getElementsByClassName('gsc-tabHeader','div',sControlDiv);
        // var affiliate = '^All News21$'
        var affiliate = '^{{ request.GET.affiliate }}$'
        var re=new RegExp(affiliate); // Local would be the string you need to replace as necessary, to correspond with the value of the tab
        for(var i=0;i<tabs.length;i++){
            var tab=tabs[i];
            if(tab.firstChild.nodeValue.match(re)){
                if(tab.dispatchEvent){
                    var ev=document.createEvent('MouseEvents');
                    ev.initEvent('click',true,true);
                    tab.dispatchEvent(ev);
                }else if(tab.fireEvent){
                    tab.fireEvent('onclick')
                }else{
                    tab.onclick()
                }
            return
            }
        }
    }

    // when the form fires a submit, grab its
    // value and call the search control
    cse.prototype.onSubmit = function(form) {
        var q = form.input.value;
        if (q && q!= "") {
            this.sControl.execute(q);
        }
    return false;
    }

    // when the form fires a clear, call the search control
    cse.prototype.onClear = function(form) {
        this.sControl.clearAllResults();
        form.input.value = "";
        return false;
    }

    function OnLoad() {
        new cse();
    }

google.setOnLoadCallback(OnLoad, true);

//]]></script>

{% endblock %}
